package com.applet.orders.controller;

import com.alibaba.fastjson.JSONObject;
import com.applet.context.AlibabaTTL;
import com.applet.dto.OrdersPageQueryDTO;
import com.applet.dto.OrdersPaymentDTO;
import com.applet.dto.OrdersSubmitDTO;
import com.applet.entity.Orders;
import com.applet.orders.service.OrderService;
import com.applet.result.PageResult;
import com.applet.result.Result;
import com.applet.vo.OrderPaymentVO;
import com.applet.vo.OrderSubmitVO;
import com.applet.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单管理 controller
 */

@Slf4j
@RestController
@RequestMapping("/user/order")
public class OrderController {


    @Resource
    private OrderService orderService;




    /**
     * 订单管理
     *        用户下单
     */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("订单管理 - 用户下单: {}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }



    /**
     * 订单管理
     *        预支付订单
     */
    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        log.info("订单管理 - 预支付订单: {}" , ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        return Result.success(orderPaymentVO);
    }




    /**
     * 订单管理
     *        查询历史订单列表
     */
    @GetMapping("/historyOrders")
    public Result<PageResult> queryPage(Integer page, Integer pageSize, Integer status){
        log.info("订单管理 - 历史订单列表查询,当前页码值: {},每页展示数: {},订单状态: {}", page, pageSize, status);
        PageResult pageResult = orderService.queryPage(page, pageSize, status);
        return Result.success(pageResult);
    }



    /**
     * 订单管理
     *        根据id查询订单详情
     */
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> getOrderAndDetailById(@PathVariable(name="id")Long id){
        log.info("订单管理 - 根据id: {}查询订单详情", id);
        OrderVO orderVO = orderService.getById(id);
        return Result.success(orderVO);
    }



    /**
     * 订单管理
     *         用户取消订单
     * - 待支付和待接单状态下，用户可直接取消订单
     * - 商家已接单状态下，用户取消订单需电话沟通商家
     * - 派送中状态下，用户取消订单需电话沟通商家
     * - 如果在待接单状态下取消订单，需要给用户退款
     * - 取消订单后需要将订单状态修改为“已取消”
     */
    @PutMapping("/cancel/{id}")
    public Result cancelOrder(@PathVariable(name="id")Long id){
        log.info("订单管理 - 订单id: {},取消订单", id);
        orderService.userCancelOrder(id);
        return Result.success();

    }



    /**
     * 订单管理
     *       订单id，再来一单
     */
    @PostMapping("/repetition/{id}")
    public Result repeatOrder(@PathVariable(name="id")Long id){
        log.info("订单管理 - 订单id: {},重新下单", id);
        orderService.repeatOrder(id);
        return Result.success();
    }



    /**
     * 订单管理
     *        用户催单  //TODO WebSocket
     */
    @GetMapping("/reminder")
    public Result userReminderOrder(@PathVariable(name="id")Long id){
        log.info("订单管理 - 用户催单,用户id: {},订单号: {},", AlibabaTTL.threadLocalGet(), id);
        JSONObject result = orderService.userReminderOrder(id);
        if(result.getInteger("code") != null && result.getInteger("code") == 1){
            return Result.success("用户催单成功");
        } else {
            return Result.error(result.toJSONString());
        }
    }




}
